from typing import Any, Optional
from mcp.server.fastmcp import FastMCP # type: ignore
from tool.weather.apihz import WeatherAPI
from tool.gitee import GitCmd

# 初始化 MCP 服务器
mcp = FastMCP("WeatherServer")

# 定义一个简单的天气查询服务

async def get_weather(province: str, city: str) -> Any:
    """获取指定地区的天气信息"""
    try:
        response = await WeatherAPI.get_weather(province, city)
        return response
    except Exception as e:
        return {"code": 500, "msg": str(e)}
    

@mcp.tool()
async def query_weather(province: str, city: str) -> Any:
    """查询指定地区的天气信息"""
    return await get_weather(province, city)

@mcp.tool()
async def upload_to_gitee(repo_path: Optional[str] = None, commit_msg: Optional[str] = None) -> Any:
    """上传代码到 Gitee 仓库
    Args:
        repo_path: 本地仓库路径，可选。如果未提供将使用环境变量 CODE_PATH
        commit_msg: 提交信息，可选。如果未提供将使用环境变量 CODE_COMMIT_MESSAGE
    Returns:
        上传结果
    """
    try:
        git = GitCmd(repo_path)
        results = await git.upload_to_gitee(commit_msg)
        
        success = all(r.success for r in results)
        steps = [{"step": i+1, "success": r.success, "message": r.message.strip()} 
                for i, r in enumerate(results)]
        
        return {
            "success": success,
            "message": "上传完成" if success else "上传失败",
            "steps": steps
        }
    except Exception as e:
        return {
            "success": False,
            "message": f"发生错误: {str(e)}",
            "steps": []
        }

if __name__ == "__main__":
    mcp.run(transport='stdio')
